package com.juc.learn.problem;

import com.juc.learn.problem.beans.Phone;
import org.junit.jupiter.api.Test;

/**
 * ClassName:TestPhone
 * Package:com.juc.learn.problem.test
 * Description:
 *
 * @Author:@郭先森
 * @Create:2025/7/27 - 9:26
 * @Version:v1.0
 */
public class TestPhone {

    //1.标准访问，是先打印短信还是先发送邮件
   /*----sendSMS
    *----sendEmail
    *
    * */
    @Test
    public void test1() throws InterruptedException {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            try {
                phone1.sendSMS();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        Thread.sleep(100);
        new Thread(()->{
            phone1.sendEmail();
        }).start();
    }

    //2.停4秒在短信方法内，先打印短信还是邮件
    /*
    *
    * ----sendSMS
    * ----sendEmail
    * */
    @Test
    public void test2() throws InterruptedException {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            try {
                phone1.sendSMS();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        Thread.sleep(100);
        new Thread(()->{
            phone1.sendEmail();
        }).start();
        Thread.sleep(5000);
    }

    //前两个例子总结：在方法上加锁，锁的是当前对象

    //3.新增普通的hello方法，是先打短信还是hello
    /*
    * 先hello，后短信
    * */
    @Test
    public void test3() throws InterruptedException {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            try {
                phone1.sendSMS();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        Thread.sleep(100);
        new Thread(()->{
            phone1.getHello();
        }).start();
        Thread.sleep(5000);
    }
    //第3个例子当前对象锁住，不会影响普通方法的执行


    //4.现在有两部手机，先打印短信还是邮件
    //先邮件后短信
    @Test
    public void test4() throws InterruptedException {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            try {
                phone1.sendSMS();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        Thread.sleep(100);
        new Thread(()->{
            phone2.sendEmail();
        }).start();
        Thread.sleep(5000);
    }
    //第4个例子可以得出，锁只能锁当前对象，两部手机，锁不住另一个手机对象

    //5.两个静态同步方法，一部手机，先打印短信还是邮件
    //先短信，后邮件
    @Test
    public void test5() throws InterruptedException {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            try {
                phone1.sendSMS1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        Thread.sleep(100);
        new Thread(()->{
            phone1.sendEmail1();
        }).start();
        Thread.sleep(5000);
    }

    //6.两个静态同步方法，两部手机，先打印短信还是手机
    //先短信，后邮件
    @Test
    public void test6() throws InterruptedException {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            try {
                phone1.sendSMS1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        Thread.sleep(100);
        new Thread(()->{
            phone2.sendEmail1();
        }).start();
        Thread.sleep(5000);
    }
    //第5、第6可以知道，锁住的不是当前对象，而是Class对象，每个类只有一个


    //7.一个静态同步方法，一个普通同步方法，一部手机，先打印短信还是邮件
    //先邮件，后短信
    @Test
    public void tes7() throws InterruptedException {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(()->{
            try {
                phone1.sendSMS1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        Thread.sleep(100);
        new Thread(()->{
            phone1.sendEmail();
        }).start();
        Thread.sleep(5000);
    }

    //8.一个静态同步方法，一个普通同步方法，两部手机，先打印短信还是邮件
    //先邮件，后短信
   @Test
   public void test8() throws InterruptedException {
       Phone phone1 = new Phone();
       Phone phone2 = new Phone();
       new Thread(()->{
           try {
               phone1.sendSMS1();
           } catch (InterruptedException e) {
               throw new RuntimeException(e);
           }
       }).start();
       Thread.sleep(100);
       new Thread(()->{
           phone2.sendEmail();
       }).start();
       Thread.sleep(5000);
   }
    //第7、第8，锁的范围是不一样的，用的不是一把锁
}
